package com.qf.main.login.realm;

import com.qf.common.base.ex.PhoneCodeException;
import com.qf.common.base.ex.ServiceException;
import com.qf.common.base.result.ResultStatus;
import com.qf.main.login.common.vo.SysPermissionVo;
import com.qf.main.login.common.vo.SysRoleVo;
import com.qf.main.login.config.LoginType;
import com.qf.main.login.entity.SysPermission;
import com.qf.main.login.entity.SysUser;
import com.qf.main.login.service.RoleService;
import com.qf.main.login.service.SysUserService;
import com.qf.main.login.token.LocalUsernamePasswordToken;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author chengqiang
 * @Create 2024-01-19-8:52
 * @Description:
 */
@Slf4j
public class SysUserRealm  extends AuthorizingRealm {
    @Resource(name = "SysUserService")
    private SysUserService sysUserService;

    @Override
    public String getName() {
        return LoginType.USER_PASSWORD.getType();
    }

    @Override
    public boolean supports(AuthenticationToken token) {
        if (token instanceof UserToken) {
            return ((UserToken) token).getLoginType() == LoginType.USER_PASSWORD;
        } else {
            return false;
        }
    }

    @Override
    public void setAuthorizationCacheName(String authorizationCacheName) {
        super.setAuthorizationCacheName(authorizationCacheName);
    }

    @Override
    protected void clearCachedAuthorizationInfo(PrincipalCollection principals) {
        super.clearCachedAuthorizationInfo(principals);
    }



    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {

        // 用户输入用户名
        String username = (String)token.getPrincipal();

        // 获取数据库中对应用户名的密码
        SysUser userInfo = sysUserService.getSysUserInfo(username);
        // System.out.println(userInfo);
        // 如果账号不存在
        if(ObjectUtils.isEmpty(userInfo)){
            throw new PhoneCodeException(ResultStatus.PHONE_DOES_NOT_EXIST);
        }
        // 如果对应用户名是否被封号
        if (userInfo.getDelStatus() != 0) {
            throw new ServiceException(ResultStatus.BANNED_USER);
        }
        return new SimpleAuthenticationInfo(userInfo, userInfo.getPassword(), null, username);
    }

    /**
     * 从数据中获取权限数据
     *
     * @param principals 认证接口的用户对象
     * @return
     */
    @Resource
    private RoleService roleService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        // 获取认证用户对象
        SysUser sysUser = (SysUser) principals.getPrimaryPrincipal();
        List<SysRoleVo> sysRoleVos = roleService.queryRoles(sysUser.getId());
//        stream流
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        //添加角色数据
        authorizationInfo.setRoles(sysRoleVos.stream().map(SysRoleVo::getRoleName).collect(Collectors.toSet()));
        //添加权限数据
        authorizationInfo.setStringPermissions(sysRoleVos.stream().flatMap(sysRoleVo -> sysRoleVo.getPermissionVoList().stream().map(SysPermissionVo::getPermissionName)).collect(Collectors.toSet()));
        return authorizationInfo;
    }

}
